package com.qen.wrapper.segments;

import cn.hutool.core.util.StrUtil;
import com.qen.enums.SqlKeyword;
import com.qen.wrapper.ISqlSegment;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Having SQL 片段
 *
 * @author licz
 * @since 1.0.0
 */
public class HavingSegmentList extends AbstractISegmentList {

    @Override
    protected boolean transformList(List<ISqlSegment> list, ISqlSegment firstSegment, ISqlSegment lastSegment) {
        if (!isEmpty()) {
            this.add(SqlKeyword.AND);
        }
        list.remove(0);
        return true;
    }

    @Override
    protected String childrenSqlSegment() {
        if (isEmpty()) {
            return StrUtil.EMPTY;
        }
        return this.stream().map(ISqlSegment::getSqlSegment).collect(Collectors.joining(StrUtil.SPACE, StrUtil.SPACE + SqlKeyword.HAVING.getSqlSegment() + StrUtil.SPACE, StrUtil.EMPTY));
    }
}
